<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/24 0024
 * Time: 14:19
 */

namespace XtwsBack\Controller;


use Think\Controller;

class ForetasteController extends Controller
{
    public function index(){
        $member = M('member');
        $goods=M('foretaste_goods');
        $count = $goods->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出

        $goods_arr =$goods->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($goods_arr as $k => $v){
            $username = $member->where('id='.$v['u_id'])->field('nickname')->find();
            $goods_arr[$k]['username'] = $v['u_id'] == 0 ? "未绑定":$username['nickname'];
            switch ($v['state']){
                case 0:
                    $goods_arr[$k]['state'] = '待审核';
                    break;
                case 1:
                    $goods_arr[$k]['state'] = '已通过';
                    break;
                case 2:
                    $goods_arr[$k]['state'] = '未通过';
                    break;
                case 3:
                    $goods_arr[$k]['state'] = '已删除';
                    break;
                case 4:
                    $goods_arr[$k]['state'] = '已结束';
                    break;
            }
        }
        $this->assign('list',$goods_arr);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }//end fun

    public function Addgroup(){
        $member = M('member');
        $category = M('category');
        $goods = M('foretaste_goods');
        //获取相关信息
        $list_category = $category->where('pid = 22 AND status = 1')->field('id,title')->select();//获取单位规格下的单位分类
        $list_foretaste = $category->where('pid = 27 AND status = 1')->field('id,title')->select();//获取单位规格下的活动分类
        $list_member = $member->field('id,u_type,nickname')->select();//获取用户列表
        //添加拼团订单到数据库
        $post_group = I("post.");
        if(IS_POST){
            $post_group['starttime'] = strtotime($post_group['starttime']);
            $post_group['endtime'] = strtotime($post_group['endtime']);
            $post_group['addtime'] = time();
            $addgoods = $goods->add($post_group);
            if ($addgoods){
                $this->success('添加成功','index.html');
                return;
            }else{
                $this->error('添加失败');
                return;
            }
        }
        $this->assign('list_member',$list_member);
        $this->assign('list_category',$list_category);
        $this->assign('list_foretaste',$list_foretaste);
        $this->display();
    }//end fun Add

    public function Savegroup(){
        $member = M('member');
        $category = M('category');
        $goods = M('foretaste_goods');
        //获取相关信息
        $list_member = $member->field('id,u_type,nickname')->select();//获取用户列表
        $list_category = $category->where('pid = 22 AND status = 1')->field('id,title')->select();//获取单位规格下的单位分类
        $list_foretaste = $category->where('pid = 27 AND status = 1')->field('id,title')->select();//获取单位规格下的活动分类
        $id = $_GET['id']?$_GET['id']:I('id');
        if($id){
            $goods_info = $goods->where('id='.$id)->find();
            //首页图分离
            $goods_info['h_imgs']=explode(",",$goods_info['h_imgs']);
            //详情图片分离
            $goods_info['c_imgs']=explode(",",$goods_info['c_imgs']);

            $goods_info['starttime'] = date('Y-m-d H:i:s',$goods_info['starttime']);
            $goods_info['endtime'] = date('Y-m-d H:i:s',$goods_info['endtime']);

            $post_group = $_POST;
            if (IS_POST){
                $post_group['starttime'] = strtotime($post_group['starttime']);
                $post_group['endtime'] = strtotime($post_group['endtime']);
                $post_group['uptime'] = time();
                foreach($post_group as $k => $v){
                    if ($v == ''){
                        if ($k != 'content'){
                            unset($post_group[$k]);
                        }
                    }
                }
                $addgroup = $goods->where('id ='.$id)->save($post_group);
                if ($addgroup){
                    $this->success('添加成功','index.html');
                    return;
                }else{
                    $this->error('添加失败');
                }
            }
            $this->assign('list_member',$list_member);
            $this->assign('goods',$goods_info);
            $this->assign('list_category',$list_category);
            $this->assign('list_foretaste',$list_foretaste);
            $this->display();
        }else{
            $this->error('参数错误');
        }
    }//end fun Save

    public function Deletegroup(){
        $goods = M('foretaste_goods');
        $id = I('id');
        if ($id){
            $delegroup = $goods->where('id='.$id)->save(['state' => 3]);
            if ($delegroup){
                $this->success('删除成功','index.html');
                return;
            }else{
                $this->error('删除失败');
            }
        }
    }//end fun Delete

}//end class